package com.wxx.service.bg;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.wxx.mapper.DicAclMapper;
import com.wxx.mapper.DicRoleAclMapper;
import com.wxx.model.DicAcl;
import com.wxx.model.DicRoleAcl;
import com.wxx.util.AppUtil;
import com.wxx.util.JsonData;
import com.wxx.vo.DicAclListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DicAclServiceImpl implements DicAclService {

    @Autowired
    private DicAclMapper dicAclMapper;
    @Autowired
    private DicRoleAclMapper dicRoleAclMapper;

    @Override
    public Map findAclByParentId(String id) {
        DicAcl model = new DicAcl();
        model.setParentId(id);
        List<DicAcl> dicAclList = dicAclMapper.select(model);
        return JsonData.success(dicAclList);
    }

    @Override
    public List<DicAcl> findTotalAcl() {
        List<DicAcl> list = dicAclMapper.findTotalAcl();
        return list;
    }

    @Override
    public List<DicAclListVo> findDicAclList() {
        List<DicAclListVo> list = dicAclMapper.findDicAclList();

        //将所有权限按照parentId分组
        Multimap<String,DicAclListVo> multimap = ArrayListMultimap.create();

        for (DicAclListVo dicAclListVo:list){
            multimap.put(dicAclListVo.getParentId(),dicAclListVo);
        }
        //获取顶级节点
        List<DicAclListVo> dicAclListVoList = (List<DicAclListVo>) multimap.get("0");

        //遍历顶级父节点，查到相应的子节点集合，并放入到该类的集合中
        for (DicAclListVo dicAclListVo : dicAclListVoList) {
            List<DicAclListVo> aclList = (List<DicAclListVo>) multimap.get(dicAclListVo.getAclId());
            for (DicAclListVo aclListVo : aclList) {
                List<DicAclListVo> aclList3 = (List<DicAclListVo>) multimap.get(aclListVo.getAclId());
                aclListVo.setList(aclList3);
            }
            dicAclListVo.setList(aclList);
        }
        //排序
        dicAclListVoList.sort((o1, o2) -> o1.getSort() - o2.getSort());

        return dicAclListVoList;
    }

    @Override
    public Map add(DicAcl dicAcl, String parentId2) {
        int flag = 0;
        DicAcl model = new DicAcl();
        //判添加的是顶级,次级权限还是按钮
        if ("0".equals(dicAcl.getParentId())){
            //添加顶级权限
            model.setParentId("0");
            model.setUrlName(dicAcl.getUrlName());
            flag = dicAclMapper.selectCount(model);
            if (flag > 0){
                return JsonData.returnMap(0,"此权限已存在");
            }

            dicAcl.setAclId(AppUtil.uuid());
            dicAcl.setCreateTime(new Date());
            dicAcl.setLevel(1);
            int i = dicAclMapper.insertSelective(dicAcl);
            return JsonData.returnMap(i);
        }else{
            //"-1".equals(parentId2) 添加的是二级菜单
            if ("-1".equals(parentId2)) {
                //添加次级权限
                model.setParentId(dicAcl.getParentId());
                model.setUrlName(dicAcl.getUrlName());
                flag = dicAclMapper.selectCount(model);
                if (flag > 0) {
                    return JsonData.returnMap(0, "此权限已存在");
                }
                dicAcl.setAclId(AppUtil.uuid());
                dicAcl.setCreateTime(new Date());
                dicAcl.setLevel(2);
                int i = dicAclMapper.insertSelective(dicAcl);
                return JsonData.returnMap(i);
            }else{
                //添加的页面按钮权限
                dicAcl.setAclId(AppUtil.uuid());
                dicAcl.setCreateTime(new Date());
                //按钮上一级是二级的菜单的id
                dicAcl.setParentId(parentId2);
                dicAcl.setLevel(3);
                int i = dicAclMapper.insertSelective(dicAcl);
                return JsonData.returnMap(i);
            }
        }
    }

    @Override
    public Map edit(DicAcl dicAcl) {
        DicAcl keyModel = dicAclMapper.selectByPrimaryKey(dicAcl.getAclId());

        DicAcl model = new DicAcl();
        if (StringUtils.isNotBlank(dicAcl.getParentId())){
            //parentId不为空 说明修改的是 次级权限，继续判断是否存在相同的权限名称，并且不是自己
            model.setParentId(dicAcl.getParentId());
            model.setUrlName(dicAcl.getUrlName());
            int flag = dicAclMapper.selectCount(model);
            if (flag > 0 && !dicAcl.getUrlName().equals(keyModel.getUrlName())){
                return JsonData.returnMap(0,"权限已存在");
            }

            dicAcl.setUpdateTime(new Date());
            int i = dicAclMapper.updateByPrimaryKeySelective(dicAcl);
            return JsonData.returnMap(i);
        }else{
            //parentId为空 说明修改的是 顶级权限，继续判断是否存在相同的权限名称，并且不是自己
            model.setParentId("0");
            model.setUrlName(dicAcl.getUrlName());
            int flag = dicAclMapper.selectCount(model);
            if (flag > 0 && !dicAcl.getUrlName().equals(keyModel.getUrlName())){
                return JsonData.returnMap(0,"权限已存在");
            }

            dicAcl.setUpdateTime(new Date());
            int i = dicAclMapper.updateByPrimaryKeySelective(dicAcl);
            return JsonData.returnMap(i);
        }
    }

    @Override
    public Map goEdit(String aclId) {
        DicAcl dicAcl = dicAclMapper.selectByPrimaryKey(aclId);

        List<DicAcl> dicAclList = Lists.newArrayList();
        //level 为3 说明有2级权限
        if (dicAcl.getLevel()==3){
            DicAcl model = new DicAcl();
            model.setLevel(2);
            dicAclList = dicAclMapper.select(model);
        }

        Map map = Maps.newHashMap();
        map.put("dicAcl",dicAcl);
        map.put("dicAclList",dicAclList);
        return map;
    }

    @Override
    public Map del(String aclId) {
        //判断是否使用了该权限，如果正在使用不能删除
        DicRoleAcl dicRoleAcl = new DicRoleAcl();
        dicRoleAcl.setAclId(aclId);
        int flag = dicRoleAclMapper.selectCount(dicRoleAcl);
        if (flag > 0){
            return JsonData.returnMap(0,"该权限正在使用，不能删除");
        }
        DicAcl dicAcl = dicAclMapper.selectByPrimaryKey(aclId);
        if (StringUtils.equals(dicAcl.getParentId(),"0")){
            //是顶级目录，先删除下面子级权限
            DicAcl model = new DicAcl();
            model.setParentId(aclId);
            dicAclMapper.delete(model);
            //在删除本权限
            dicAclMapper.deleteByPrimaryKey(aclId);
        }else{
            dicAclMapper.deleteByPrimaryKey(aclId);
        }

        return JsonData.success();
    }
}
